﻿Imports Capa_de_Acceso_a_Datos
Public Class frmRegistrarOrdenPago

    Public FarmaciaId As Integer = 0
    Public frmLlamada As String

    Private Sub frmRegistrarOrdenPago_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cargarGrillaPresentaciones()
    End Sub

    Private Sub cargarGrillaPresentaciones()
        Dim presentaciones As DataTable = PresentacionDAO.obtenerPresentacionesAPagar(FarmaciaId)
        dgvPresentaciones.DataSource = presentaciones
        dgvPresentaciones.Refresh()
    End Sub

    Private Sub btnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
        frmSeleccionarFarmacia.frmLlamada = Me.Name
        frmSeleccionarFarmacia.ShowDialog()
    End Sub

    Private Sub btnBuscarPresentaciones_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscarPresentaciones.Click
        If FarmaciaId <> 0 Then
            cargarGrillaPresentaciones()
        Else
            MsgBox("Farmacia es un dato requerido", MsgBoxStyle.Exclamation)
        End If
    End Sub

    Private Sub btnPagar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPagar.Click
        Dim fila As DataGridViewRow
        Dim dt As DataTable = New DataTable
        limpiar()
        dt.Columns.Add("ColumnaBaseImponible")
        For Each fila In dgvPresentaciones.Rows
            If fila.Cells("ColumnaCheck").Value = True Then
                dt.Rows.Add(fila.Cells("ColumnaBaseImponible").Value)
            End If
        Next

        calcularBaseImponible(dt)
    End Sub

    Private Sub limpiar()

    End Sub

    Private Sub calcularBaseImponible(ByVal tablaPagar As DataTable)
        Dim fila As DataRow
        Dim baseImponible As Double = 0
        For Each fila In tablaPagar.Rows
            baseImponible = baseImponible + CDbl(fila("ColumnaBaseImponible").ToString())
        Next
        txtBase.Text = Utilidades.darFormatoDouble(baseImponible)
        calcularRetenciones(baseImponible)

    End Sub

    Private Sub calcularRetenciones(ByVal baseImponible As Double)
        Dim retIng As Double = 0
        Dim retGan As Double = 0
        Dim retMuni As Double = 0
        retIng = baseImponible * 0.024
        retGan = baseImponible * 0.024
        retMuni = baseImponible * 0.024
        txtGanancias.Text = Utilidades.darFormatoDouble(retIng)
        txtIngrerosBrutos.Text = Utilidades.darFormatoDouble(retGan)
        txtMunicipalidad.Text = Utilidades.darFormatoDouble(retMuni)
        txtTotalPagar.Text = Utilidades.darFormatoDouble(baseImponible - (retIng + retGan + retMuni))
    End Sub

    Private Sub btnSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalir.Click
        Me.Dispose()
    End Sub

    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub
End Class